<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px" style="padding-right: 1px;">
      <el-row>
        <el-col :span="8">
          <el-form-item label="组织名称" prop="deptId" style="width: 100%;">
            <el-tree-select
              v-model="queryParams.deptId"
              :data="deptOptions"
              :props="{ value: 'deptId', label: 'deptName', children: 'children' }"
              value-key="deptId"
              placeholder="请选择组织名称"
              check-strictly
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="姓名" prop="name" style="width: 100%;">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入姓名"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="性别" style="width: 100%;">
              <el-select v-model="queryParams.sex" placeholder="请选择">
                <el-option
                    v-for="dict in sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row>
        <el-col :span="8">
          <el-form-item label="人员类别" prop="memberType" style="width: 100%;">
            <el-select v-model="queryParams.memberType" placeholder="请选择人员类别" clearable>
              <el-option
                v-for="dict in sys_member_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="手机号" prop="phonenumber" style="width: 100%;">
            <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="状态" prop="status" style="width: 100%;">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
              <el-option
                v-for="dict in sys_normal_disable"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <!--<el-row>
        <el-col :span="8">
          <el-form-item label="备注" prop="remark" style="width: 100%;">
            <el-input
              v-model="queryParams.remark"
              placeholder="请输入备注"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>-->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['partybuilding:Member:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
            icon="Upload"
            @click="handleImport"
            v-hasPermi="['partybuilding:Member:import']"
        >导入</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          icon="Download"
          @click="handleExport"
          v-hasPermi="['partybuilding:Member:export']"
        >导出</el-button>
      </el-col>

      <!--<el-col :span="1.5">
        <el-button
            icon="Delete"
            @click="handleDelete"
            v-hasPermi="['partybuilding:Member:remove']"
        >批量删除</el-button>
      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" @handleQuery="handleQuery" @resetQuery="resetQuery"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="MemberList" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column fixed label="党组织名称" align="center" prop="deptName" width="150"/>
      <el-table-column fixed label="姓名" align="center" prop="name" width="150"/>
      <el-table-column label="手机号" align="center" prop="phonenumber" width="150"/>
      <el-table-column label="性别" align="center" prop="sex" width="90">
        <template #default="scope">
            <el-tag v-if="scope.row.sex==0">男</el-tag>
            <el-tag v-else>女</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="民族" align="center" prop="nation" width="150"/>
      <el-table-column label="出生日期" align="center" prop="birth" width="150">
        <template #default="scope">
            <span v-if="scope.row.memberStart!=null">{{ subTimeStr(scope.row.birth, 0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文化程度" align="center" prop="education" width="150"/>
      <el-table-column label="党员类别" align="center" prop="memberType" width="150">
        <template #default="scope">
            <el-tag v-if="scope.row.memberType==1">正式党员</el-tag>
            <el-tag v-else>预备党员</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="入党时间" align="center" prop="memberStart" width="150">
        <template #default="scope">
            <span v-if="scope.row.memberStart!=null">{{ subTimeStr(scope.row.memberStart, 0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="转正时间" align="center" prop="confirmationTime" width="150">
        <template #default="scope">
          <span v-if="scope.row.confirmationTime!=null">{{ subTimeStr(scope.row.confirmationTime, 0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="90">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="党籍状态" align="center" prop="memberStatus" width="90">
        <template #default="scope">
          <dict-tag :options="sys_member_status" :value="scope.row.memberStatus" />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" min-width="200">
        <template #default="scope">
          <el-button link type="danger" @click="handleUpdate(scope.row)" v-hasPermi="['partybuilding:Member:edit']">修改</el-button>
          <el-divider direction="vertical" />
          <el-button link type="danger" @click="toRepaireResume(scope.row)" v-hasPermi="['partybuilding:Member:edit']">履历</el-button>
          <el-divider direction="vertical" />
          <el-button link type="danger" @click="handleDelete(scope.row)" v-hasPermi="['partybuilding:Member:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改党员信息对话框 -->
    <el-dialog :title="title" v-model="open" width="900px" append-to-body style="margin-top: 13px!important;">
      <el-form ref="memberRef" :model="form" :rules="rules" label-width="109px">
        <el-row>
          <div style="font-size: 18px;font-family: PingFangSC-Regular;color: #333;position: relative;top: 15px;left: 6px;">基本信息</div>
          <el-divider />
          <el-col :span="12">
            <el-form-item label="用户名" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户名" :disabled="form.id!=null" @change="handleUserNameChange" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="code">
              <el-input v-model="form.code" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="form.sex">
                <el-radio v-for="dict in sys_user_sex" 
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="民族" prop="nation">
              <el-select v-model="form.nation" placeholder="请选择民族">
                  <el-option
                      v-for="dict in pb_member_nation"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                  ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="籍贯" prop="homeTown">
              <el-cascader
                :options="regionData"
                v-model="homeTownArray"
                @change="handleHomeTownChange"
                placeholder="请选择籍贯"
                clearable
                style="width: 100%;">
              </el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birth">
              <el-date-picker clearable
                v-model="form.birth"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择出生日期"
                style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学历" prop="education">
              <el-select v-model="form.education" placeholder="请选择学历">
                  <el-option
                      v-for="dict in pb_member_education"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                  ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作岗位" prop="position">
              <el-select v-model="form.position" placeholder="请选择工作岗位">
                  <el-option
                      v-for="dict in pb_member_job"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                  ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否退休" prop="retire">
              <el-radio-group v-model="form.retire">
                <el-radio v-for="dict in retireList" 
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in sys_normal_disable" 
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="家庭住址" prop="address">
              <el-input v-model="form.address" placeholder="请输入家庭住址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="照片" prop="picture">
              <image-upload
                  v-model="form.picture"
                  :multiple="true"
                  :limit="1"
                  :file-type="['jpg', 'png', 'jpeg']"
                  :file-size="200">
              </image-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <div style="font-size: 18px;font-family: PingFangSC-Regular;color: #333;position: relative;top: 15px;left: 6px;">党员信息</div>
          <el-divider />
          <el-col :span="12">
            <el-form-item label="所属党组织" prop="deptId" style="width: 100%;">
              <el-tree-select
                v-model="form.deptId"
                :data="deptOptions"
                :props="{ value: 'deptId', label: 'deptName', children: 'children' }"
                value-key="deptId"
                placeholder="请选择所属党组织"
                check-strictly
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="人员类别" prop="memberType">
              <el-radio-group v-model="form.memberType">
                <el-radio v-for="dict in sys_member_type" 
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="党籍状态" prop="memberStatus">
              <el-radio-group v-model="form.memberStatus">
                <el-radio v-for="dict in sys_member_status" 
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入党时间" prop="memberStart">
              <el-date-picker clearable
                v-model="form.memberStart"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择入党时间"
                style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="转正时间" prop="confirmationTime">
              <el-date-picker clearable
                v-model="form.confirmationTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择转正时间"
                style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" :rows="8"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 修改履历对话框 -->
    <el-dialog :title="titleResume" v-model="openResume" width="924px" append-to-body>
      <el-form ref="memberResumeRef" :model="form" :rules="rules" label-width="0px" label-position="center" style="padding: 19px 9px 1px 9px;">
        <el-row>
          <el-col :span="24">
            <el-form-item label="" prop="resume" style="width: 100%;">
              <editor v-model="form.resume" :height="600"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitResumeForm">确 定</el-button>
          <el-button @click="cancelResume">取 消</el-button>
        </div>
      </template>
    </el-dialog>


    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
        <el-upload
          ref="uploadRef"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url + '?updateSupport=' + upload.updateSupport"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
        >
          <el-icon class="el-icon--upload"><upload-filled /></el-icon>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <template #tip>
              <div class="el-upload__tip text-center">
                <span>仅允许导入xls、xlsx格式文件。</span>
                <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
              </div>
          </template>
        </el-upload>
        <template #footer>
          <div class="dialog-footer">
              <el-button type="primary" @click="submitFileForm">确 定</el-button>
              <el-button @click="upload.open = false">取 消</el-button>
          </div>
        </template>
    </el-dialog>

  </div>
</template>

<script setup name="Member">
import { getToken } from "@/utils/auth";
import { listOrg } from "@/api/partybuilding/Org";
import { listMember, getMember, delMember, addMember, updateMember, editResume, checkInfo } from "@/api/partybuilding/Member";
import { regionData, codeToText} from 'element-china-area-data'
const { proxy } = getCurrentInstance();

const MemberList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptOptions = ref(undefined);
const openResume = ref(false);
const titleResume = ref("");
const homeTownArray = ref([]);

const { sys_user_sex } = proxy.useDict("sys_user_sex");
const { pb_member_job } = proxy.useDict("pb_member_job");
const { pb_member_nation } = proxy.useDict("pb_member_nation");
const { pb_member_education } = proxy.useDict("pb_member_education");
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
const sys_member_status = ref([{label: '正常', value: '1'}, {label: '停止', value: '0'}]);
const sys_member_type = ref([{label: '预备', value: '0'}, {label: '正式', value: '1'}]);
const retireList = ref([{label: '未退休', value: '0'}, {label: '退休', value: '1'}]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    code: null,
    nation: null,
    homeTown: null,
    birth: null,
    education: null,
    address: null,
    position: null,
    retire: null,
    picture: null,
    userId: null,
    memberType: null,
    memberStart: null,
    memberStatus: null,
  },
  rules: {
    userName: [
      { required: true, message: "用户名不能为空", trigger: ["change", "blur"] }
    ],
    phonenumber: [
      { validator: (rule, value, callback) => {
          if (value && !/^[1][3,4,5,7,8][0-9]{9}$/.test(value)) { 
            callback(new Error("请输入正确的手机号码")); 
          } else {
            callback(); 
          }
        }, 
        trigger: ["change", "blur"] 
      }
    ],
    code: [
      { validator: (rule, value, callback) => {
          if (value==null || value=="") {
            callback();
          }
          let aCity = {
            11: '北京',
            12: '天津',
            13: '河北',
            14: '山西',
            15: '内蒙古',
            21: '辽宁',
            22: '吉林',
            23: '黑龙江',
            31: '上海',
            32: '江苏',
            33: '浙江',
            34: '安徽',
            35: '福建',
            36: '江西',
            37: '山东',
            41: '河南',
            42: '湖北',
            43: '湖南',
            44: '广东',
            45: '广西',
            46: '海南',
            50: '重庆',
            51: '四川',
            52: '贵州',
            53: '云南',
            54: '西藏',
            61: '陕西',
            62: '甘肃',
            63: '青海',
            64: '宁夏',
            65: '新疆',
            71: '台湾',
            81: '香港',
            82: '澳门',
            91: '国外'
          };
        
          if (!/^\d{17}(\d|x)$/i.test(value)) {
            callback(new Error('格式错误，请输入正确的身份证号'));
            return;
          }
        
          value = value.replace(/x$/i, 'a');
          if (aCity[parseInt(value.substr(0, 2))] == null) {
            callback(new Error('格式错误，请输入正确的身份证号'));
            return;
          }
        
          const sBirthday = value.substr(6, 4) + '-' + Number(value.substr(10, 2)) + '-' + Number(value.substr(12, 2));
          const d = new Date(sBirthday.replace(/-/g, '/'));
          if (sBirthday !== d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()) {
            callback(new Error('请输入正确的身份证号'));
            return;
          }
        
          let iSum = 0;
          for (let i = 17; i >= 0; i--) {
            iSum += (Math.pow(2, i) % 11) * parseInt(value.charAt(17 - i), 11);
          }
          if (iSum % 11 != 1) {
            callback(new Error('请输入正确的身份证号'));
            return;
          }
          callback();
        }, 
        trigger: ["change", "blur"] 
      }
    ],
    name: [
      { required: true, message: "姓名不能为空", trigger: ["change", "blur"] }
    ],
    memberType: [
      { required: true, message: "请选择人员类别", trigger: ["change", "blur"] }
    ],
    birth: [{ required: true, message: "请选择出生日期", trigger: ["change", "blur"] }],
    sex: [{ required: true, message: "请选择性别", trigger: ["change", "blur"] }],
    deptId: [{ required: true, message: "请选择所属党组织", trigger: ["change", "blur"] }],
    nation: [{ required: true, message: "请选择民族", trigger: ["change", "blur"] }],
    education: [{ required: true, message: "请选择学历", trigger: ["change", "blur"] }],
    memberStart: [{ required: true, message: "请选择入党时间", trigger: ["change", "blur"] }],
  }
});
const { queryParams, form, rules } = toRefs(data);

/*** 党员导入参数 */
const upload = reactive({
  // 是否显示弹出层（党员导入）
  open: false,
  // 弹出层标题（党员导入）
  title: "",
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 0,
  // 设置上传的请求头部
  headers: { Authorization: "Bearer " + getToken() },
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "/partybuilding/Member/importData"
});
/** 导入按钮操作 */
function handleImport() {
  upload.title = "党员导入";
  upload.open = true;
};

/** 下载模板操作 */
function importTemplate() {
  proxy.download("/partybuilding/Member/importTemplate", {
  }, `member_template_${new Date().getTime()}.xlsx`);
};

/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
  upload.isUploading = true;
};

/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
  upload.open = false;
  upload.isUploading = false;
  proxy.$refs["uploadRef"].handleRemove(file);
  proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
  getList();
};

/** 提交上传文件 */
function submitFileForm() {
  proxy.$refs["uploadRef"].submit();
};

/** 查询部门下拉树结构 */
function getDeptTree() {
  listOrg().then(response => {
    deptOptions.value = proxy.handleTree(response.rows, "deptId");
  });
};

function handleHomeTownChange(val) {
  if (val!=null && val.length > 0) {
    form.value.homeTown = val.join(',');
  }else{
    form.value.homeTown = null;
  }  
}

function getCodeToText(codeArray) {
  let area = "";
  switch (codeArray.length) {
    case 1:
      area += codeToText[codeArray[0]];
      break;
    case 2:
      area += codeToText[codeArray[0]] + "/" + codeToText[codeArray[1]];
      break;
    case 3:
      area +=
        codeToText[codeArray[0]] +
        "/" +
        codeToText[codeArray[1]] +
        "/" +
        codeToText[codeArray[2]];
      break;
    default:
      break;
  }
  console.log(area);
}

/** 查询党员信息列表 */
function getList() {
  loading.value = true;
  queryParams.value.orderByName = "id";
  queryParams.value.descIs = true;
  listMember(queryParams.value).then(response => {
    MemberList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    code: null,
    nation: null,
    homeTown: null,
    birth: null,
    education: null,
    address: null,
    position: null,
    retire: null,
    picture: null,
    userId: null,
    memberType: null,
    memberStart: null,
    memberStatus: null,
    remark: null,
    resume: null
  };
  form.value.resume = '<p></p>';
  proxy.resetForm("memberRef");
}

/** 时间格式化 */
function subTimeStr(dateStr) {
  if (dateStr == null || dateStr == "") {
    return "";
  }
  return dateStr.substring(0, 10);
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

// 进入修改履历弹出框
function toRepaireResume(row) {
  reset()
  form.value.id = row.id;
  form.value.resume = row.resume? row.resume : '<p></p>';
  openResume.value = true;
  titleResume.value = "编辑履历";
}

// 监听用户名输入框
function handleUserNameChange(){
  if (form.value.userName){
    let uName = form.value.userName;
    checkInfo({userName:uName}).then(response => {
      if (response.data!= null && response.data==500) {
        proxy.$message.error("用户名【"+form.value.userName+"】已存在，请重新输入");
        form.value.userName = "";
      }
    });
  }
}

//   提交履历表单
function submitResumeForm() {
  proxy.$refs["memberResumeRef"].validate(valid => {
    if (valid) {
      editResume(form.value).then(response => {
        proxy.$modal.msgSuccess("修改成功");
        openResume.value = false;        
        getList();
      });
    }
  });
}

// 取消履历弹出框
function cancelResume() {
  openResume.value = false;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  form.value.sex = '0';
  form.value.status = '0';
  form.value.memberType = '1';
  form.value.memberStatus = '1';
  form.value.retire = '0';
  open.value = true;
  title.value = "添加党员信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getMember(_id).then(response => {
    form.value = response.data;
    form.value.phonenumber=response.data.user.phonenumber;
    form.value.userName=response.data.user.userName;
    form.value.sex=response.data.user.sex+'';
    form.value.status=response.data.user.status+'';
    form.value.retire=response.data.retire+'';
    form.value.memberType=response.data.memberType+'';
    form.value.memberStatus=response.data.memberStatus+'';
    form.value.deptId=response.data.user.deptId;
    homeTownArray.value = response.data.homeTown!= null ? response.data.homeTown.split(',') : [];
    open.value = true;
    title.value = "修改党员信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["memberRef"].validate(valid => {
    if (valid) {
      let user={};
      user.phonenumber=form.value.phonenumber;
      user.userName=form.value.userName;
      user.sex=form.value.sex;
      user.status=form.value.status;
      user.nickName = form.value.name;
      if (form.value.deptId != null) {
        user.deptId = form.value.deptId;
      }
      form.value.user = user;
      if (form.value.memberStart!= null) {
        form.value.memberStart = form.value.memberStart + " 00:00:00";
      }
      if (form.value.confirmationTime!= null) {
        form.value.confirmationTime = form.value.confirmationTime + " 00:00:00";
      }
      if (form.value.id != null) {
        updateMember(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMember(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除党员信息的数据项？').then(function() {
    return delMember(row.userId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('partybuilding/Member/export', {
    ...queryParams.value
  }, `Member_${new Date().getTime()}.xlsx`)
}

getDeptTree();
getList();
</script>
